package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Attendance;
import cn.wolfcode.trip.base.domain.OrderByUserId;
import cn.wolfcode.trip.base.domain.Product;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.AttendanceMapper;
import cn.wolfcode.trip.base.mapper.OrderByUserIdMapper;
import cn.wolfcode.trip.base.mapper.ProductByUserMapper;
import cn.wolfcode.trip.base.query.IntegralQuery;
import cn.wolfcode.trip.base.service.IAttendanceService;
import cn.wolfcode.trip.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AttendanceServiceImpl implements IAttendanceService {

    @Autowired
    private AttendanceMapper attendanceMapper;
    @Autowired
    private OrderByUserIdMapper orderByUserIdMapper;
    @Autowired
    private ProductByUserMapper productByUserMapper;

    @Override
    public Integer saveOrUpdate(Attendance attendance, Date date) {
        if(attendance.getId()==null){
            attendance.setSignNum(1);
            attendance.setIntegral(10);
            attendance.setUser(UserContext.getCurrentUser());
            String signTimes = attendance.getSignTimes();
            signTimes=signTimes+date.getDate()+";";
            attendance.setSignTimes(signTimes);
            attendanceMapper.insert(attendance);
            return attendance.getSignNum();
        }else{
            long days=(date.getTime()-attendance.getLastSignTime().getTime())/86400000;
            int num=0;
            if(days>1){
                num=1;
                attendance.setSignNum(1);
            }else {
                Integer signNum = attendance.getSignNum();
                attendance.setSignNum(signNum+1);
                num=signNum+1;
            }
            Integer integral = attendance.getIntegral();
            if(attendance.getSignNum()==7){
                num=7;
                attendance.setSignNum(1);
                integral+=50;
            }
            attendance.setIntegral(integral+10);
            String signTimes = attendance.getSignTimes();
            signTimes=signTimes+date.getDate()+";";
            attendance.setSignTimes(signTimes);
            attendanceMapper.updateByPrimaryKey(attendance);
            return num;
        }
    }

    @Override
    public Attendance get(Long userId) {
        Attendance attendance = attendanceMapper.selectByPrimaryKey(userId);
        if(attendance==null){
            return new Attendance();
        }
        return attendance;
    }

    @Override
    public void pay(IntegralQuery qo,Long[] productId,int[] productNumber,Long[] carId) {
        User currentUser = UserContext.getCurrentUser();
        System.out.println(qo.getTotal());
        Attendance attendance = attendanceMapper.selectByPrimaryKey(qo.getUserId());
        Integer integral=Integer.valueOf(attendance.getIntegral());
        Integer tatal=Integer.valueOf(qo.getTotal());
        if(integral>tatal){
            attendance.setIntegral(integral-tatal);
            attendanceMapper.updateByPrimaryKey(attendance);
            for(int i=0;i<productId.length;i++){
                Product p=new Product();
                p.setId(productId[i]);
                OrderByUserId order=new OrderByUserId();
                order.setProductNumber(productNumber[i]);
                order.setProduct(p);
                order.setUser(currentUser);
                orderByUserIdMapper.insert(order);
               productByUserMapper.deleteById(currentUser.getId(),carId[i]);
            }
        }else {
            throw new RuntimeException("您的积分不足,请及时充值!");
        }
    }

}
